﻿// Copyright 2010 Maxime Serrano <lastchronicler@gmail.com>
using System;
using System.IO;

namespace MCSharp.Network.Handlers
{
    class BlockDigHandler : PacketHandler
    {
        #region PacketHandler Members

        public void Handle(BinaryReader reader, Session session, Player player)
        {
            // Permission check
            if (!player.Can(Permission.Delete))
            {
                player.Message(Color.Red + "You are not allowed to delete blocks!");
                return;
            }

            // Read in data
            byte status = reader.ReadByte();
            int blockX = PacketWriter.SwapEndian(reader.ReadUInt32());
            byte blockY = reader.ReadByte();
            int blockZ = PacketWriter.SwapEndian(reader.ReadUInt32());
            /* byte dir = */ reader.ReadByte();

            // Destroy block (if broken or in Instant Break mode)
            if (status == 3 || player.InstantBreak)
                player.DestroyBlock(blockX, blockY, blockZ);
        }

        #endregion
    }

    public class BlockPlaceHandler : PacketHandler
    {

        #region PacketHandler Members

        public void Handle(BinaryReader reader, Session session, Player player)
        {
            // Permission check
            if (!player.Can(Permission.Build))
            {
                player.Message(Color.Red + "You are not allowed to place blocks!");
                return;
            }

            // Read in data
            short type = PacketWriter.SwapEndian(reader.ReadInt16());
            int blockX = PacketWriter.SwapEndian(reader.ReadUInt32());
            byte blockY = reader.ReadByte();
            int blockZ = PacketWriter.SwapEndian(reader.ReadUInt32());
            byte dir = reader.ReadByte();

            // Place block
            player.PlaceBlock((byte)type, blockX, blockY, blockZ, dir);
        }

        #endregion
    }
}
